<?php

class Editor extends input
{
	private $libPath;

	public function __construct($id, $libPath = null )
	{
        parent::__construct();
        $this->id = $id;
        $this->libPath = $libPath ? $libPath : LIBPATH ; //if don't pass try to get constant
	}

	function __toString()
	{
		$id                     = $this->id;
        $this->id               = null; //to not render in element
        $styles                 = (array) $this->getStyle(); //converted to array
        $libPath                = $this->libPath;
        $this->libPath          = null;
		$attributes             = get_object_vars($this);
		//clean unnecessary attributes
        $attributes['style']    = null;
        $attributes['inner']    = null;
        $attributes['tagName']  = null;
        $attributes['id']       = null;
        $attributes['class']    = null;
        $attributes['label']    = null;
        $attributes['type']     = null;

        //attributes are passed to js object
		if ( is_array( $attributes ))
		{
			foreach ( $attributes as $attribute => $value )
			{
				if ( $value )
				{
                    $this->$attribute = null; //clean attribute in object
					$attribute = ucfirst( $attribute );
                    $script .= "object_{$id}.$attribute = '$value';";
				}
			}
		}

		//style are passed to frame
	    if ( is_array( $styles ))
        {
            foreach ( $styles as $style => $value )
            {
                if ( $value )
                {
                    $scriptStyle .= "{$id}___Frame.style.$style = '$value';";
                }
            }
        }

        $js ="
        var object_{$id}= new FCKeditor( '$id' ) ;
	    object_{$id}.BasePath    = '$libPath'; ;
        $script
	    object_{$id}.Create() ;
        ";

        if ( $scriptStyle )
        {
        	$js .="var {$id}___Frame = document.getElementById('{$id}___Frame'); $scriptStyle";
        }

        $script = new Script( $js );

        return $script->__toString();
	}
}
?>